#include "GrappleHook.h"
#include "../SGD Wrappers/SGD_Geometry.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "GameplayState.h"
#include "Game.h"
#include "ParticleSystem.h"
#include "GrappleGun.h"
#include "ObjectManager.h"
#include "Arrow.h"
#include "Player.h"
#include "../SGD Wrappers/SGD_AudioManager.h"

GrappleHook::GrappleHook(BaseObject* Owner)
{
	m_pOwner = Owner;

	m_hImage = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/graphics/Hook.png");
	SetImage(m_hImage);

	m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Hook_Hit.wav");

	SGD::InputManager* input = SGD::InputManager::GetInstance();
	

	// Check to see if a controller is connected, otherwise use mouse input
	if (!input->IsControllerConnected(0))
	{
		SetVelocity(SGD::Vector(input->GetMousePosition().x - (m_pOwner->GetPosition().x - GameplayState::GetInstance()->GetCamX()),
			input->GetMousePosition().y - (m_pOwner->GetPosition().y - GameplayState::GetInstance()->GetCamY())));
		GetVelocity().Normalize();
		SetVelocity(GetVelocity() * 5);
	}
	else
	{
		Arrow* arrow = dynamic_cast<Player*>(Owner)->GetArrow();

		SGD::Vector heading;

		heading = { 1 * dynamic_cast<Player*>(Owner)->GetScale().width, 0 };
			heading.Rotate(arrow->GetRotation()* dynamic_cast<Player*>(Owner)->GetScale().width);
		
		SetVelocity(heading*1000);
	}


	SetPosition(m_pOwner->GetPosition());
	newPosition = m_ptPosition;



	SetAlive(true);

	ParticleSystem ps;
	ps.Load("resource/savefiles/GrapHook.xml", ps, *this);
	SetParticle(ps);

	SGD::Vector tempVec;
	SGD::Point mouseLoc = SGD::InputManager::GetInstance()->GetMousePosition();
	tempVec.x = mouseLoc.x;
	tempVec.y = mouseLoc.y;

	SetRotation(SGD::Vector(0,1).ComputeAngle(tempVec));
	SetSize(SGD::Size(32, 32));


}

GrappleHook::~GrappleHook()
{
	ReleaseAll();
}

void GrappleHook::ReleaseAll()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hImage);
	SGD::AudioManager::GetInstance()->UnloadAudio(m_aAudio);

	for (unsigned int j = 0; j < m_vtParticle.size(); j++)
	{
		m_vtParticle[j]->GetEmitter()->GetAliveVec().clear();

		while (m_vtParticle[j]->GetEmitter()->GetFlyweight()->GetRefCount() > 1)
			m_vtParticle[j]->GetEmitter()->GetFlyweight()->Release();

		delete m_vtParticle[j];
	}
	m_vtParticle.clear();

	while (m_unRefCount > 1)
		m_unRefCount--;

	Release();
}

void GrappleHook::Render()
{
	SGD::GraphicsManager::GetInstance()->DrawTexture(
		m_hImage, SGD::Point(m_ptPosition.x - GameplayState::GetInstance()->GetCamX(), m_ptPosition.y - GameplayState::GetInstance()->GetCamY()),
		m_fRotation, SGD::Vector(m_szSize.width / 2, m_szSize.height / 2));
}

void GrappleHook::HandleCollision(BaseObject* pOther)
{
	if (dynamic_cast<StaticObject*>(pOther)->GetObjectType() == ObjectType::Hookable)
	{
		if (!hasHit)
		{
			hasHit = true;
			m_ptPosition.x = pOther->GetPosition().x + pOther->GetSize().width / 2;
			m_ptPosition.y = pOther->GetPosition().y + pOther->GetSize().height / 2;
			SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
		}
	}
	else if (dynamic_cast<StaticObject*>(pOther)->GetObjectType() == ObjectType::GROUND)
	{
		SGD::Event* e = new SGD::Event("Kill_Grap", nullptr, m_pOwner);
		e->QueueEvent();
	}
}

void GrappleHook::Update(float elapsedTime)
{

	if (!hasHit)
	{
		newPosition.x = (GetPosition().x + GetVelocity().x * elapsedTime);
		newPosition.y = GetPosition().y + GetVelocity().y * elapsedTime;
		SetPosition(newPosition);
		m_fDestroyTimer = 0.0f;
		if (m_vtParticle.size() != 0)
			m_vtParticle[0]->Update(elapsedTime);
	}

}
